package yhao.infra.common.threads;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置信息
 * @Author zhao bin
 **/
public class ThreadPoolConfig {

    private final int availableProcessors = Runtime.getRuntime().availableProcessors();

    public static ThreadPoolConfig defaultConfig() {
        return new ThreadPoolConfig();
    }

    // 核心保留时间，单位second
    private int aliveSeconds = 60;

    // 核心线程数， 默认为CPU核心数
    private int corePoolSize = availableProcessors;

    // 最大线程数，默认为CPU核心数
    private int maxPoolSize = availableProcessors;

    // 队列，默认容量为int最大值
    private BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(Integer.MAX_VALUE);

    // 拒绝策略
    private RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();

    public int getCorePoolSize() {
        return corePoolSize;
    }

    public ThreadPoolConfig setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
        return this;
    }

    public int getMaxPoolSize() {
        return maxPoolSize;
    }

    public ThreadPoolConfig setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
        return this;
    }

    public BlockingQueue<Runnable> getWorkQueue() {
        return workQueue;
    }

    public ThreadPoolConfig setWorkQueue(BlockingQueue<Runnable> workQueue) {
        this.workQueue = workQueue;
        return this;
    }

    public RejectedExecutionHandler getHandler() {
        return handler;
    }

    public ThreadPoolConfig setHandler(RejectedExecutionHandler handler) {
        this.handler = handler;
        return this;
    }

    public int getAliveSeconds() {
        return aliveSeconds;
    }

    public ThreadPoolConfig setAliveSeconds(int aliveSeconds) {
        this.aliveSeconds = aliveSeconds;
        return this;
    }
}
